// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var _css = require("./lendUnitInfoList.css");
	var router = require("../hash/pageRouter");
	var html_lendUnit_create = require("./html_lendUnit_create.html");
	var tools = require("tools");
	var databind = require("databind");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		loadLendUnitList();

		initEvent();
	};
	var initEvent = function() {
		$("#lendUnitInfoList .blue_search_btn").unbind("click").click(function(){
			loadLendUnitList();
		});
		
		$("#lendUnitInfoList .createLendUnit").unbind("click").click(function(){
			$("#popdialog").html(html_lendUnit_create);
			dialog.showDialog($("#create_lendUnit_dialog"));
			$("#create_lendUnit_dialog .dialogHandlerBtn").unbind("click").click(function(){
				databind.submitDom($("#create_lendUnit_dialog"), function(){
					base.alert(1, "新增成功！");
					dialog.hideDialog($("#create_lendUnit_dialog"));
					router.route("lendUnitInfoManage/lendUnitInfoList.html");
				});
			});
		});
	};
	
	var loadLendUnitList = function() {
		//搜索条件初始化
		var oData = {
			lendUnitName: $("#lendUnitInfoList input[name=lendUnitName]").val(),
			lendUnitBankCard: $("#lendUnitInfoList input[name=lendUnitBankCard]").val(),
			lendUnitBank: $("#lendUnitInfoList input[name=lendUnitBank]").val(),
			pageIndex: pageIndex,
			pageSize: pageSize
		};
		tools.loadList("inquireLendUnitInfoPagelist.json", $("#lendUnitInfoList .table"), $("#lendUnitInfoTpl"), oData, function(){
			//启用出借单位			
			$(".use").unbind("click").click(function(){
				var $this = $(this);
				dialog.showConfirmDialog("启用出借单位", "是否确定启用该出借单位？", function(){
					useLendUnitInfoDetail($this.parents("tr").attr("lendUnitId"));
				});
			});
			
			//停用出借单位
			$(".stop").unbind("click").click(function(){
				var $this = $(this);
				dialog.showConfirmDialog("停用出借单位", "是否确定停用该出借单位？", function(){
					stopLendUnitInfoDetail($this.parents("tr").attr("lendUnitId"));
				});
			});
			
			//列表编辑
			$("#lendUnitInfoList .edit").unbind("click").click(function(){
				var $this = $(this);
				$("#popdialog").html(html_lendUnit_create);
				dialog.showDialog($("#create_lendUnit_dialog"));
				$("#create_lendUnit_dialog").attr("action", "updateLendUnitInfo");
				$("#create_lendUnit_dialog").find(".dialogTitle").html("编辑出借单位")
				$("#create_lendUnit_dialog").find("input[name=lendUnitId]").val($this.parents("tr").attr("lendUnitId"));
				databind.fillInput1($("#create_lendUnit_dialog"), {
					action: "inquireLendUnitInfoDetail.json?lendUnitId=" + $this.parents("tr").attr("lendUnitId")
				}, function(){
					$("#create_lendUnit_dialog input[name=lendUnitName]").attr("disabled", "disabled");
				});
				$("#create_lendUnit_dialog .dialogHandlerBtn").unbind("click").click(function(){
					databind.submitDom($("#create_lendUnit_dialog"), function(){
						base.alert(1, "新增成功！");
						dialog.hideDialog($("#create_lendUnit_dialog"));
						router.route("lendUnitInfoManage/lendUnitInfoList.html");
					});
				});
			});
		});
	};
	
	// 启用
	var useLendUnitInfoDetail = function(lendUnitId) {
		base.fnAjax({
			action: "useLendUnitInfoDetail.json", //接口名称
			oData: {
				lendUnitId: lendUnitId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert(1, "确认成功！");
					router.route("lendUnitInfoManage/lendUnitInfoList.html");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	// 停用
	var stopLendUnitInfoDetail = function(lendUnitId) {
		base.fnAjax({
			action: "stopLendUnitInfoDetail.json", //接口名称
			oData: {
				lendUnitId: lendUnitId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert(1, "停用成功！");
					router.route("lendUnitInfoManage/lendUnitInfoList.html");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	return {
		init: init
	};
});